/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DialogWijzigen.java
 *
 * Created on 15-feb-2012, 13:08:28
 */

package my.StudentAdministratie;

import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;

import my.StudentAdministratie.XMLWrapper.XMLException;

/**
 *
 * @author thuis
 */
public class DialogWijzigen extends javax.swing.JFrame {
	private static final long serialVersionUID = 3214875836222284083L;
	public static final String DATE_FORMAT_NOW = "dd-MM-yyyy";

	private XMLWrapper xmlWrapper;
	private Student student;

	private String naam;
	private String woonplaats;
	private String adres;
	private String postcode;
	private String telefoonnr;
	private String geslacht;
	private String inschrijfDatum;

	/** Creates new form DialogWijzigen */
	public DialogWijzigen(XMLWrapper xmlWrapper, Student student) {
		this.xmlWrapper = xmlWrapper;
		this.student = student;

		if(student != null && student.studentnummer != null && !student.studentnummer.equals(""))
		{
			initComponents();

			tfStudentNummer.setEnabled(false);
			tfNaam.setText(student.naam);
			tfStudentNummer.setText(student.studentnummer);
			tfWoonplaats.setText(student.woonplaats);
			tfAdres.setText(student.adres);
			tfPostcode.setText(student.postcode);
			tfTelefoonNummer.setText(student.telefoonnummer);

			ButtonGroup bg = new ButtonGroup();
			bg.add(RadioM);
			bg.add(RadioV);

			if(student.geslacht.equals("M"))
			{
				RadioM.setSelected(true);
			}
			else if(student.geslacht.equals("V"))
			{
				RadioV.setSelected(true);
			}
		} else {
			JOptionPane.showMessageDialog(this, "Geen student om te wijzigen opgegeven!", "Fout", JOptionPane.ERROR_MESSAGE);
		}

	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		buttonGroup1 = new javax.swing.ButtonGroup();
		buttonGroup2 = new javax.swing.ButtonGroup();
		buttonGroup3 = new javax.swing.ButtonGroup();
		tfWoonplaats = new javax.swing.JTextField();
		tfNaam = new javax.swing.JTextField();
		jLabel6 = new javax.swing.JLabel();
		tfStudentNummer = new javax.swing.JTextField();
		jLabel7 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jLabel3 = new javax.swing.JLabel();
		tfAdres = new javax.swing.JTextField();
		tfPostcode = new javax.swing.JTextField();
		buWijzigen = new javax.swing.JButton();
		jLabel1 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		jLabel5 = new javax.swing.JLabel();
		jLabel9 = new javax.swing.JLabel();
		tfTelefoonNummer = new javax.swing.JTextField();
		RadioM = new javax.swing.JRadioButton();
		RadioV = new javax.swing.JRadioButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		jLabel6.setText("Adres");

		jLabel7.setText("Postcode");

		jLabel2.setText("Studentnummer");

		jLabel3.setText("Woonplaats");

		buWijzigen.setText("Wijzigen");
		buWijzigen.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				buWijzigenActionPerformed(evt);
			}
		});

		jLabel1.setText("Naam");

		jLabel4.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
		jLabel4.setText("Wijzigen student");

		jLabel5.setText("Telefoonnummer");

		jLabel9.setText("Geslacht");

		RadioM.setText("M");

		RadioV.setText("V");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
								.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
										.addContainerGap()
										.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
												.addGap(40, 40, 40)
												.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(jLabel2)
														.addComponent(jLabel3)
														.addComponent(jLabel6)
														.addComponent(jLabel7)
														.addComponent(jLabel1)
														.addComponent(jLabel5)
														.addComponent(jLabel9))
														.addGap(85, 85, 85)
														.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
																.addGroup(layout.createSequentialGroup()
																		.addComponent(RadioM)
																		.addGap(42, 42, 42)
																		.addComponent(RadioV))
																		.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
																				.addComponent(tfTelefoonNummer, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)
																				.addComponent(tfNaam, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)
																				.addComponent(tfWoonplaats, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)
																				.addComponent(tfPostcode, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)
																				.addComponent(tfAdres, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)
																				.addComponent(buWijzigen, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)
																				.addComponent(tfStudentNummer, javax.swing.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)))))
																				.addGap(26, 26, 26))
				);
		layout.setVerticalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
								.addGroup(layout.createSequentialGroup()
										.addComponent(jLabel4)
										.addGap(51, 51, 51))
										.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(tfNaam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(jLabel1)))
												.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel2)
														.addComponent(tfStudentNummer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
														.addGap(18, 18, 18)
														.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
																.addComponent(jLabel3)
																.addComponent(tfWoonplaats, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
																.addGap(18, 18, 18)
																.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
																		.addComponent(jLabel6)
																		.addComponent(tfAdres, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
																		.addGap(18, 18, 18)
																		.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
																				.addComponent(jLabel7)
																				.addComponent(tfPostcode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
																				.addGap(18, 18, 18)
																				.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
																						.addComponent(tfTelefoonNummer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(jLabel5))
																						.addGap(18, 18, 18)
																						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
																								.addComponent(jLabel9)
																								.addComponent(RadioM)
																								.addComponent(RadioV))
																								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
																								.addComponent(buWijzigen)
																								.addGap(21, 21, 21))
				);

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void buWijzigenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
		naam = tfNaam.getText();
		woonplaats = tfWoonplaats.getText();
		adres = tfAdres.getText();
		postcode = tfPostcode.getText();
		telefoonnr = tfTelefoonNummer.getText();


		if (RadioM.isSelected())
			geslacht = "M";
		if(RadioV.isSelected())
			geslacht = "V";
		
		if(naam.equals(""))
			JOptionPane.showMessageDialog(this, "Vul aub een naam in!");
		else {
			//Make sure to edit the old student, to make the edits visable right away
			this.student.naam = naam;
			this.student.adres = adres;
			this.student.postcode = postcode;
			this.student.woonplaats = woonplaats;
			this.student.geslacht = geslacht;
			this.student.telefoonnummer = telefoonnr;
			
			try {
				xmlWrapper.writeStudent(student, true);
				this.dispose();
			} catch(XMLException e) {
				JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
			}
		}

	}//GEN-LAST:event_jButton3ActionPerformed

	/**
	 * @param args the command line arguments
	 *
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DialogWijzigen().setVisible(true);
            }
        });
    }
	 */

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JRadioButton RadioM;
	private javax.swing.JRadioButton RadioV;
	private javax.swing.ButtonGroup buttonGroup1;
	private javax.swing.ButtonGroup buttonGroup2;
	private javax.swing.ButtonGroup buttonGroup3;
	private javax.swing.JButton buWijzigen;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JLabel jLabel7;
	private javax.swing.JLabel jLabel9;
	private javax.swing.JTextField tfNaam;
	private javax.swing.JTextField tfStudentNummer;
	private javax.swing.JTextField tfWoonplaats;
	private javax.swing.JTextField tfAdres;
	private javax.swing.JTextField tfPostcode;
	private javax.swing.JTextField tfTelefoonNummer;
	// End of variables declaration//GEN-END:variables

}
